package cn.wolfcode.wolf2w.redis.service.impl;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import cn.wolfcode.wolf2w.util.RedisKeys;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IStrategyService strategyService;
    @Override
    public void queryViewNum(String id) {
//     //获取vokey;
  //    String vokey = RedisKeys.STRATEGY_STATIC_VO.join(id);
//        //判断vo是否存在
//        StrategyStatisVO vo = null;
//        if (!redisTemplate.hasKey(vokey)) {
//            vo = new StrategyStatisVO();
//            Strategy strategy = strategyService.get(id);
//            BeanUtils.copyProperties(strategy, vo);
//            vo.setStrategyId(strategy.getId());
//        } else {
//            String voStr = redisTemplate.opsForValue().get(vokey);
//            vo = JSON.parseObject(voStr,StrategyStatisVO.class);
//        }
        StrategyStatisVO vo = this.find(id);
        vo.setViewnum(vo.getViewnum() + 1);
        this.setStrategyStatisVO(vo);
    }

    @Override
    public StrategyStatisVO find(String sid) {
        //获取vokey;
        String vokey = RedisKeys.STRATEGY_STATIC_VO.join(sid);
        //判断vo是否存在
        StrategyStatisVO vo = null;
        if (!redisTemplate.hasKey(vokey)) {
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
        } else {
            String voStr = redisTemplate.opsForValue().get(vokey);
            vo = JSON.parseObject(voStr,StrategyStatisVO.class);
        }
        return vo;
    }

    @Override
    public void setStrategyStatisVO(StrategyStatisVO vo) {
        String vokey = RedisKeys.STRATEGY_STATIC_VO.join(vo.getStrategyId());
        redisTemplate.opsForValue().set(vokey,JSON.toJSONString(vo));
    }

    @Override
    public void queryReplynum(String id) {
        StrategyStatisVO vo = this.find(id);
        vo.setReplynum(vo.getReplynum() + 1);
        this.setStrategyStatisVO(vo);
    }

    @Override
    public boolean queryFavornum(String sid, String uid) {
        String fkey = RedisKeys.USER_FAVOR.join(uid);
        List<String> uidList = null;
        if (redisTemplate.hasKey(fkey)) {
            String str = redisTemplate.opsForValue().get(fkey);
            uidList  = JSON.parseArray(str,String.class);
        } else {
            uidList = new ArrayList();
            String str = JSON.toJSONString(uidList);
            redisTemplate.opsForValue().set(fkey,str);
        }
        StrategyStatisVO vo = this.find(sid);
        if (uidList.contains(sid)) {
            vo.setFavornum(vo.getFavornum() - 1);
            uidList.remove(sid);
        } else {
            vo.setFavornum(vo.getFavornum() + 1);
            this.setStrategyStatisVO(vo);
            uidList.add(sid);
        }
        redisTemplate.opsForValue().set(fkey,JSON.toJSONString(uidList));
        this.setStrategyStatisVO(vo);
        return uidList.contains(sid);
    }

    @Override
    public boolean queryStrategyThumbup(String sid, String uid) {
        String tkey = RedisKeys.STRATEGY_USER_THUM.join(uid,sid);
        //获取当前时间
        Date date = new Date();
        Date endDate = DateUtil.getEndDate(date);
        long time = DateUtil.getDateBetween(endDate, date);
        //判断tkey是否存在
        if (redisTemplate.hasKey(tkey)){
             return false;
        }
        redisTemplate.opsForValue().set(tkey,"1",time,TimeUnit.SECONDS);
        StrategyStatisVO vo = this.find(sid);
        vo.setThumbsupnum(vo.getThumbsupnum() + 1);
        this.setStrategyStatisVO(vo);
        return true;
    }

    @Override
    public boolean isVoexists(String id) {
        return !redisTemplate.hasKey(RedisKeys.STRATEGY_STATIC_VO.join(id));
    }
}
